package com.mes.web.controller.biz;

import java.util.List;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.validation.constraints.*;

import com.mes.biz.domain.BizProduct;
import com.mes.biz.service.BizProductService;
import com.mes.common.utils.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.mes.common.annotation.Log;
import com.mes.common.core.domain.R;
import com.mes.common.core.domain.TreeNode;
import com.mes.common.enums.BusinessType;
import com.mes.common.constant.Constants;
import com.mes.framework.cache.DictUtils;
import com.mes.common.core.controller.BaseController;
import com.mes.biz.domain.BizProductCategory;
import com.mes.biz.service.BizProductCategoryService;

/**
 * 产品分类Controller
 * @author hwg
 * @email hwg
 * @date 2023-04-16
 */
@RestController
@RequestMapping("/biz/bizProductCategory")
public class BizProductCategoryController extends BaseController
{
    @Autowired
    private BizProductCategoryService bizProductCategoryService;
    @Resource
    private BizProductService bizProductService;

    /**
     * 查询产品分类树列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizProductCategory:list')")
    @GetMapping("/list/{level}/{id}")
    public R list(@PathVariable("level") @NotEmpty int level, @PathVariable("id") String id) {
        if (level == 0) {
            level = 2;
        }
        //默认为根节点
        if (StringUtils.isEmpty(id)) {
            id = Constants.TREE_ROOT;
        }
        List<BizProductCategory> bizProductCategoryList = bizProductCategoryService.listDataByLevel(level, id);
        return R.data(bizProductCategoryList);
    }

	/**
    * 查询产品分类树结构
    * @param level
    * @return
    */
    @PreAuthorize("@ss.hasPermi('biz:bizProductCategory:list')")
    @GetMapping("/listTree/{level}/{id}")
    public R listTree(@NotBlank @PathVariable("level") int level, @PathVariable("id") String id) {
        if (level == 0) {
            level = 2;
        }
        //默认为根节点
        if (StringUtils.isEmpty(id)) {
            id = Constants.TREE_ROOT;
        }
        List<TreeNode> bizProductCategoryList = bizProductCategoryService.buildTree(level, id);
        return R.data(bizProductCategoryList);
    }

	/**
    * 查询产品分类树结构（排除当前节点及子节点）
    * @param level
    * @return
    */
    @PreAuthorize("@ss.hasPermi('biz:bizProductCategory:list')")
    @GetMapping("/listTreeExcludeChild/{level}/{id}/{excludeId}")
    public R listTreeExcludeChild(@NotBlank @PathVariable("level") int level, @PathVariable("id") String id, @PathVariable("excludeId") String excludeId) {
        if (level == 0) {
            level = 2;
        }
        //默认为根节点
        if (StringUtils.isEmpty(id)) {
            id = Constants.TREE_ROOT;
        }
        BizProductCategory bizProductCategory = bizProductCategoryService.get(excludeId);
        List<TreeNode> bizProductCategoryList = bizProductCategoryService.buildTreeDataExcludeChild(level, id, bizProductCategory);
        return R.data(bizProductCategoryList);
    }

    /**
     * 获取产品分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizProductCategory:query')")
    @GetMapping(value = "/{id}")
    public R<BizProductCategory> detail(@PathVariable("id") String id)
    {
        return R.data(bizProductCategoryService.get(id));
    }

    /**
     * 新增产品分类
     */
    @PreAuthorize("@ss.hasPermi('biz:bizProductCategory:add')")
    @Log(title = "产品分类", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  BizProductCategory bizProductCategory)
    {
        bizProductCategory.setTenantId(SecurityUtils.getTenantId());
        bizProductCategoryService.save(bizProductCategory);
        return R.data(bizProductCategory);
    }

    /**
     * 修改产品分类
     */
    @PreAuthorize("@ss.hasPermi('biz:bizProductCategory:edit')")
    @Log(title = "产品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated BizProductCategory bizProductCategory)
    {
        bizProductCategoryService.save(bizProductCategory);
        return R.data(bizProductCategory);
    }

    /**
     * 删除产品分类
     */
    @PreAuthorize("@ss.hasPermi('biz:bizProductCategory:remove')")
    @Log(title = "产品分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R remove(@PathVariable  String id) {
        BizProduct bizProduct = new BizProduct();
        bizProduct.setCategoryId(id);
        if(bizProductService.findList(bizProduct).isEmpty()) {
            BizProductCategory bizProductCategory = new BizProductCategory();
            bizProductCategory.setId(id);
            return R.status(bizProductCategoryService.remove(bizProductCategory));
        }
        return R.fail("产品分类存在关联产品，无法删除");
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizProductCategory:query')")
    @GetMapping("/findMaxSort/{parentId}")
    public R findMaxSort(@PathVariable  String parentId)
    {
        BizProductCategory bizProductCategory = new BizProductCategory();
        bizProductCategory.setParentId(parentId);
		return R.data(bizProductCategoryService.findMaxSort(bizProductCategory));
    }

    /**
     * 产品分类树表格检索
     */
    @PreAuthorize("@ss.hasPermi('biz:bizProductCategory:list')")
    @GetMapping("/searchBizProductCategoryList")
    public R searchBizProductCategoryList (BizProductCategory bizProductCategory)
    {
        List<BizProductCategory> bizProductCategoryList =  bizProductCategoryService.searchBizProductCategoryList(bizProductCategory);
        return R.data(bizProductCategoryList);
    }

	/**
     * 产品分类树检索
     */
    @PreAuthorize("@ss.hasPermi('biz:bizProductCategory:list')")
    @GetMapping("/searchBizProductCategory")
    public R searchBizProductCategory (BizProductCategory bizProductCategory)
    {
        List<TreeNode> bizProductCategoryList =  bizProductCategoryService.searchBizProductCategory(bizProductCategory);
        return R.data(bizProductCategoryList);
    }


    /**
     * 根据字典类型查询字典数据信息等其他自定义信息
     */
    @GetMapping(value = "/getInitData/{dictTypes}")
    public R getInitData(@PathVariable String dictTypes) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.putAll(DictUtils.getMultiDictList(dictTypes));
        return R.data(dataMap);
    }

    @GetMapping("/appletListTree/{level}/{id}")
    public R appletListTree(@NotBlank @PathVariable("level") int level, @PathVariable("id") String id) {
        if (level == 0) {
            level = 2;
        }
        //默认为根节点
        if (StringUtils.isEmpty(id)) {
            id = Constants.TREE_ROOT;
        }
        List<TreeNode> bizProductCategoryList = bizProductCategoryService.buildTree(level, id);
        return R.data(bizProductCategoryList);
    }
}
